package com.benbi.shequapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.benbi.shequapi.bean.Buildin;
import com.benbi.shequapi.bean.Building;
import com.benbi.shequapi.bean.Community;
import com.benbi.shequapi.bean.Livein;
import com.benbi.shequapi.mapper.BuildinMapper;
import com.benbi.shequapi.mapper.BuildingMapper;
import com.benbi.shequapi.mapper.CommunityMapper;
import com.benbi.shequapi.mapper.LiveinMapper;
import com.benbi.shequapi.utils.ResultData;

public class ServiceUtil {

    /**
     * errorCode:
     * 1: 没有此居民
     * 2: 未知错误
     * @param liveinMapper mapper
     * @param buildingMapper mapper
     * @param buildinMapper mapper
     * @param communityMapper mapper
     * @param pid pid
     * @return 居住位置 resultData.data:{
     *     "bno", 楼号String,
     *     "room", 房号String,
     *     "cname", 社区名String
     * }
     */
    public static ResultData getLiveAddrByPid(LiveinMapper liveinMapper,
                                              BuildingMapper buildingMapper,
                                              BuildinMapper buildinMapper,
                                              CommunityMapper communityMapper,
                                              int pid){
        ResultData liveResultData = getLiveDataByPid(liveinMapper, buildingMapper, buildinMapper, communityMapper, pid);
        if(!liveResultData.isSuccess()) {return liveResultData;}

        JSONObject liveData = (JSONObject) liveResultData.getData();
        Livein li = (Livein) liveData.get("livein");
        Building b = (Building) liveData.get("building");
        Community c = (Community) liveData.get("community");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bno", b.getBno());
        jsonObject.put("room", li.getRoom());
        jsonObject.put("cname", c.getCname());
        return new ResultData(jsonObject);
    }

    /**
     * errorCode:
     * 1: 没有此居民
     * 2: 未知错误
     * @param liveinMapper mapper
     * @param buildingMapper mapper
     * @param buildinMapper mapper
     * @param communityMapper mapper
     * @param pid pid
     * @return 查询住址所需的所有数据 resultData.data:{
     *     "livein": livein,
     *     "building": building,
     *     "buildin": buildin,
     *     "community": community
     * }
     */
    public static ResultData getLiveDataByPid(LiveinMapper liveinMapper,
                                              BuildingMapper buildingMapper,
                                              BuildinMapper buildinMapper,
                                              CommunityMapper communityMapper,
                                              int pid){
        Livein li = liveinMapper.selectLiveinByPid(pid);
        if (li == null){ return new ResultData(1, "没有此居民"); }

        Building b = buildingMapper.selectBuildingByBid(li.getBid());
        Buildin bi = buildinMapper.selectBuildinByBid(li.getBid());
        if (bi == null || b == null){ return new ResultData(2, "未知错误"); }

        Community c = communityMapper.selectCommunityByCid(bi.getCid());
        if (bi == null){ return new ResultData(2, "未知错误"); }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("livein", li);
        jsonObject.put("building", b);
        jsonObject.put("buildin", bi);
        jsonObject.put("community", c);
        return new ResultData(jsonObject);
    }


}
